//
//  PhotoBrowserController.swift
//  DXWeibo
//
//  Created by dx on 17/2/22.
//  Copyright © 2017年 dx. All rights reserved.
//

import UIKit

/// 照片浏览控制器 - 负责用户交互
class PhotoBrowserController: UIViewController {

    let selectedIndex: Int
    let urls: [String]
    
    let animator: PhotoBrowserAnimator
    
    init(selectedIndex: Int, urls: [String]) {
        self.selectedIndex = selectedIndex
        self.urls = urls
        
        // 创建动画器
        animator = PhotoBrowserAnimator()
        
        super.init(nibName: nil, bundle: nil)
        
        // 指定转场动画代理(weak)
        transitioningDelegate = animator
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        print(selectedIndex)
        print(urls)
        
        setupUI()
    }
    
    // MARK: - 监听方法
    @objc fileprivate func tapGesture(recognizer: UITapGestureRecognizer) {
        dismiss(animated: true, completion: nil)
    }
}

// MARK: - UIPageViewControllerDataSource
extension PhotoBrowserController: UIPageViewControllerDataSource {
    
    /// 返回前一页控制器！
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        
        // 从控制器中获取当前图片的索引
        var index = (viewController as! PhotoViewerController).photoIndex
        
        // 判断是是否到头
        if index <= 0 {
            return nil
        }
        index -= 1
        
        // 创建并且返回 PhotoViewerController
        return PhotoViewerController(urlString: urls[index], photoIndex: index)
    }
    
    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        var index = (viewController as! PhotoViewerController).photoIndex
        if index >= urls.count - 1 {
            return nil
        }
        index += 1
        return PhotoViewerController(urlString: urls[index], photoIndex: index)
    }
}

// MARK: - 设置界面
extension PhotoBrowserController {
    
    fileprivate func setupUI() {
        view.backgroundColor = UIColor.orange
        
        // 分页控制器
        // 1> 实例化
        let pageViewController = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: [UIPageViewControllerOptionInterPageSpacingKey: 20])
        // 2> 添加分页控制器的子控制器 setViewControllers
        // * 单图控制器 - 分页控制器初始显示的控制器
        let viewer = PhotoViewerController(urlString: urls[selectedIndex], photoIndex: selectedIndex)
        pageViewController.setViewControllers([viewer], direction: .forward, animated: false, completion: nil)
        
        // 3> 将分页控制器的视图和控制器，添加到当前控制器上
        view.addSubview(pageViewController.view)
        addChildViewController(pageViewController)
        pageViewController.didMove(toParentViewController: self)
        
        // 4> 设置手势识别
        view.gestureRecognizers = pageViewController.gestureRecognizers
        
        // 5> 数据源
        pageViewController.dataSource = self
        
        // 手势监听
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapGesture))
        view.addGestureRecognizer(tap)
    }
}
